package com.commerce.reader.Activity;

import android.app.AlertDialog;
import android.graphics.Color;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.AdapterView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.SeekBar;
import android.widget.TextView;

import androidx.appcompat.app.AppCompatActivity;
import androidx.drawerlayout.widget.DrawerLayout;

import com.commerce.reader.Entity.Book;
import com.commerce.reader.Entity.Chapter;
import com.commerce.reader.R;
import com.commerce.reader.adapter.MyListAdapter;

import org.litepal.LitePal;

import java.util.List;

import static com.commerce.reader.utils.JsoupHtml.toBook;


public class ReadActivity extends AppCompatActivity implements View.OnClickListener {

    private LinearLayout menuLayout;
    private TextView contentText;
    private View viewMode;
    private TextView titleText;
    private TextView chapterTextpre;
    private TextView chapterTextnext;
    private SeekBar contentProgress;
    private TextView categoryText;
    private TextView tip;
    private TextView nightText;
    private TextView settingText;
    private ListView categoryListView;
    private DrawerLayout drawerLayout;
    private List<Chapter> chapter;
    private int id;
    private String book;
    private String name;
    private AlertDialog alertDialog;
    private Book bookLitepal;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_read);

        initView();

    }

    private void initView() {

        drawerLayout = findViewById(R.id.read_dl_category);
        viewMode = findViewById(R.id.read_vw_mode);
        titleText = findViewById(R.id.read_tv_title_text);
        menuLayout = findViewById(R.id.read_ll_bottom_menu);
        contentText = findViewById(R.id.read_tv_content_text);

        chapterTextnext = findViewById(R.id.read_tv_next_chapter);
        chapterTextpre = findViewById(R.id.read_tv_pre_chapter);
        contentProgress = findViewById(R.id.read_sb_chapter_progress);
        tip = findViewById(R.id.read_tv_page_tip);

        categoryText = findViewById(R.id.read_tv_category);
        nightText = findViewById(R.id.read_tv_night_mode);
        settingText = findViewById(R.id.read_tv_setting);

        categoryListView = findViewById(R.id.read_iv_category);


        chapterTextnext.setOnClickListener(this);
        chapterTextpre.setOnClickListener(this);
        categoryText.setOnClickListener(this);
        nightText.setOnClickListener(this);
        settingText.setOnClickListener(this);
        contentText.setOnClickListener(this);
        id = getIntent().getIntExtra("ids", 0);
        book = getIntent().getStringExtra("book");
        name = getIntent().getStringExtra("name");
        bookLitepal =(Book)getIntent().getSerializableExtra("bookLitepal");
        chapter = LitePal.where("idName = ?", book + name).find(Chapter.class);
        categoryListView.setAdapter(new MyListAdapter(this, chapter));
        categoryListView.requestFocus();
        categoryListView.setSelection(id);
        chapterLoad(0);

        contentProgress.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
            @Override
            public void onProgressChanged(SeekBar seekBar, int i, boolean b) {
                if (b) {
                    id = (int) (i / 100.00 * chapter.size());
                    tip.setText(id + "/" + chapter.size());
                }

            }

            @Override
            public void onStartTrackingTouch(SeekBar seekBar) {
                tip.setVisibility(View.VISIBLE);
            }

            @Override
            public void onStopTrackingTouch(SeekBar seekBar) {
                tip.setVisibility(View.GONE);
                chapterLoad(0);
            }
        });
        categoryListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
                id = (int) l;
                chapterLoad(0);
                drawerLayout.close();
                menuLayout.setVisibility(View.GONE);
            }
        });
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.read_tv_next_chapter:
                chapterLoad(1);
                break;
            case R.id.read_tv_pre_chapter:
                chapterLoad(2);
                break;
            case R.id.read_tv_category:
                drawerLayout.open();
                break;
            case R.id.read_tv_night_mode:
                if (viewMode.getVisibility() == View.VISIBLE) {
                    viewMode.setVisibility(View.GONE);
                } else {
                    viewMode.setVisibility(View.VISIBLE);
                }
                break;
            case R.id.read_tv_setting:
                showCustomizeDialog();
                break;
            case R.id.read_tv_content_text:
                int state = menuLayout.getVisibility();
                if (state == View.VISIBLE) {
                    menuLayout.setVisibility(View.GONE);
                } else {
                    menuLayout.setVisibility(View.VISIBLE);
                }
                break;

        }
    }

    private void showCustomizeDialog() {
        AlertDialog.Builder customizeDialog =
                new AlertDialog.Builder(this);
        final View dialogView = LayoutInflater.from(this)
                .inflate(R.layout.dialog_customize, null);
        customizeDialog.setTitle("我是一个自定义Dialog");
        initDialog(dialogView);
        customizeDialog.setView(dialogView);
        alertDialog = customizeDialog.create();
        alertDialog.show();
    }

    private void initDialog(View view) {
        View mDcColor1 = view.findViewById(R.id.dc_color_1);
        mDcColor1.setBackgroundColor(Color.CYAN);
        mDcColor1.setOnClickListener(new DialogClick());
        View mDcColor2 = view.findViewById(R.id.dc_color_2);
        mDcColor2.setBackgroundColor(Color.GREEN);
        mDcColor2.setOnClickListener(new DialogClick());
        View mDcColor3 = view.findViewById(R.id.dc_color_3);
        mDcColor3.setBackgroundColor(Color.BLUE);
        mDcColor3.setOnClickListener(new DialogClick());
        View mDcColor4 = view.findViewById(R.id.dc_color_4);
        mDcColor4.setBackgroundColor(Color.parseColor("#009688"));
        mDcColor4.setOnClickListener(new DialogClick());
        View mDcSizeMin = view.findViewById(R.id.dc_size_min);
        mDcSizeMin.setOnClickListener(new DialogClick());
        View mDcSizeMin2 = view.findViewById(R.id.dc_size_min2);
        mDcSizeMin2.setOnClickListener(new DialogClick());
        View mDcSizeMax = view.findViewById(R.id.dc_size_max);
        mDcSizeMax.setOnClickListener(new DialogClick());
        View mDcSizeMax2 = view.findViewById(R.id.dc_size_max2);
        mDcSizeMax2.setOnClickListener(new DialogClick());

    }

    private  class DialogClick implements View.OnClickListener {

        @Override
        public void onClick(View view) {
            alertDialog.cancel();
            switch (view.getId()) {
                case R.id.dc_color_1 :
                    drawerLayout.setBackgroundColor(Color.CYAN);
                    categoryListView.setBackgroundColor(Color.CYAN);
                    break;
                case R.id.dc_color_2:
                    drawerLayout.setBackgroundColor(Color.GREEN);
                    categoryListView.setBackgroundColor(Color.GREEN);
                    break;
                case R.id.dc_color_3 :
                    drawerLayout.setBackgroundColor(Color.BLUE);
                    categoryListView.setBackgroundColor(Color.BLUE);
                    break;
                case R.id.dc_color_4 :
                    drawerLayout.setBackgroundColor(Color.parseColor("#009688"));
                    categoryListView.setBackgroundColor(Color.parseColor("#009688"));
                    break;
                case R.id.dc_size_min :
                    contentText.setTextSize(15);
                    break;
                case R.id.dc_size_min2 :
                    contentText.setTextSize(20);
                    break;
                case R.id.dc_size_max :
                    contentText.setTextSize(25);
                    break;
                case R.id.dc_size_max2 :
                    contentText.setTextSize(30);
                    break;
            }

        }
    }

    private void chapterLoad(final int b) {
        new Thread(new Runnable() {
            @Override
            public void run() {
                runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        contentText.setText("");
                    }
                });
                if (b == 1) {
                    id++;
                } else if (b == 2) {
                    id--;
                }
                if (id >= chapter.size()) {
                    id = chapter.size() - 1;
                }
                if (id < 0) {
                    id = 0;
                }
                final String s = toBook(chapter.get(id).getUrl());
                runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        contentText.setText(s);
                        titleText.setText(chapter.get(id).getChapter());
                        Log.d("TAG", "run3333: " + id / (double) chapter.size());
                        contentProgress.setProgress((int) (id / (double) chapter.size() * 100));
                    }
                });
            }
        }).start();
    }

    @Override
    protected void onStop() {
        bookLitepal.setIds(id);
        bookLitepal.updateAll("name = ? and book = ?",name,book);
        super.onStop();
    }
}